package com.nd.service.wx;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.TopicMapper;
import com.nd.pojo.bean.Coupon;
import com.nd.pojo.bean.Topic;
import com.nd.pojo.bean.TopicExample;
import com.nd.pojo.vo.wx.CouponDataVo;
import com.nd.pojo.vo.wx.TopicDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TopicWxServiceImpl implements TopicWxService {


    @Autowired
    TopicMapper topicMapper;

    @Override
    public CouponDataVo list(Integer page, Integer size) {

        PageHelper.startPage(page, size);

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();

        List<Topic> topics = topicMapper.selectByExample(topicExample);

        PageInfo<Topic> userPageInfo = new PageInfo<>(topics);
        long total = userPageInfo.getTotal();

        CouponDataVo couponDataVo = new CouponDataVo();

        couponDataVo.setCount((int)total);
        couponDataVo.setData(topics);


        return couponDataVo;
    }

    @Override
    public TopicDetailVo detail(Integer id) {

        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();
        TopicExample.Criteria criteria1 = criteria.andIdEqualTo(id);

        List<Topic> topics = topicMapper.selectByExampleWithBLOBs(topicExample);

//        topics.get(0).setContent("<img src=\"//yanxuan.nosdn.127.net/75c55a13fde5eb2bc2dd6813b4c565cc.jpg\">     " +
//                "<img src=\"//yanxuan.nosdn.127.net/e27e1de2b271a28a21c10213b9df7e95.jpg\">     " +
//                "<img src=\"//yanxuan.nosdn.127.net/9d413d1d28f753cb19096b533d53418d.jpg\">     " +
//                "<img src=\"//yanxuan.nosdn.127.net/64b0f2f350969e9818a3b6c43c217325.jpg\">    " +
//                " <img src=\"//yanxuan.nosdn.127.net/a668e6ae7f1fa45565c1eac221787570.jpg\">     " +
//                "<img src=\"//yanxuan.nosdn.127.net/0d4004e19728f2707f08f4be79bbc774.jpg\">     " +
//                "<img src=\"//yanxuan.nosdn.127.net/79ee021bbe97de7ecda691de6787241f.jpg\">");

        TopicDetailVo topicDetailVo = new TopicDetailVo();

        topicDetailVo.setTopic(topics.get(0));
        topicDetailVo.setGoods(topics.get(0).getGoods());


        return topicDetailVo;
    }

    @Override
    public List<Topic> related(Integer id) {
        // 专题相关：取出和id对应的标题和副标题，和数据库所有的topics的标题和副标题
        // 查到的是 标题相似 或 副标题相似的
        // 取出和id对应的标题和副标题
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria criteria = topicExample.createCriteria();

        criteria.andIdEqualTo(id);
        List<Topic> topics = topicMapper.selectByExample(topicExample);

        String title = topics.get(0).getTitle();
        String subtitle = topics.get(0).getSubtitle();


        // 取所有topic比较标题副标题
        TopicExample topicExample1 = new TopicExample();
        TopicExample.Criteria criteria1 = topicExample1.createCriteria();
        List<Topic> topics1 = topicMapper.selectByExampleWithBLOBs(topicExample1);

        List<Topic> newtopics = new ArrayList<>();

//        for (Topic topic : topics1) {
//
//            if (!topic.getId().equals(id)) {
//                if (topic.getTitle().contains(title) || title.contains(topic.getTitle())){
//                    newtopics.add(topic);
//                }
//                if (topic.getSubtitle().contains(subtitle) || subtitle.contains(topic.getSubtitle()) ){
//                    newtopics.add(topic);
//                }
//            }
//        }

        Integer count = 0;
        for (Topic topic : topics1) {
            count++;
            newtopics.add(topic);
            if(count == 4){
                break;
            }

        }

        return newtopics;
    }
}
